package com.kong.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kong.constant.MessageConstant;
import com.kong.entity.Result;
import com.kong.pojo.Setmeal;
import com.kong.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

import static com.kong.constant.RedisConstant.SETMEAL_ALL;
import static com.kong.constant.RedisConstant.SETMEAL_DETAIL;

/**
 * @author yang2013
 * @data 2020/10/14 17:51
 */
@RestController
@RequestMapping("/mobilesetmeal")
public class SetmealMobileController {

    @Reference
    private SetmealService setmealService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 查询套餐详情---不适合使用缓存
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findSetMealDetailById(Integer id){
        try {
            Jedis jedis = jedisPool.getResource();
            String key=String.format(SETMEAL_DETAIL,id);
            String detail = jedis.hget(key, String.valueOf(id));
            ObjectMapper mapper=new ObjectMapper();
            Setmeal setmeal=null;
            if (!StringUtils.isEmpty(detail)){
                setmeal = mapper.readValue(detail, Setmeal.class);
            }else {
                setmeal = setmealService.findById(id);
                String value = mapper.writeValueAsString(setmeal);
                jedis.hset(key,String.valueOf(id),value);
            }
            return new Result(true,MessageConstant.QUERY_SETMEALLIST_FAIL,setmeal);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /**
     * 使用fastjson将字符串转换为集合对象
     * 使用redis缓存套餐和套餐详情--套餐详情使用hash--套餐列表使用string存储
     * 每次先从redis中读取数据
     * @return
     */
    @PostMapping("/getsetmeal")
    public Result getSetmeal(){
        try {
            List<Setmeal> all=null;
            Jedis jedis = jedisPool.getResource();
            ObjectMapper mapper = new ObjectMapper();
            String allSetmeal = jedis.get(SETMEAL_ALL);
            if (allSetmeal==null){
                all = setmealService.findAll();
                String value = mapper.writeValueAsString(all);
                jedis.set(SETMEAL_ALL,value);
            }else {
                JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class, Setmeal.class);
                all=mapper.readValue(allSetmeal,javaType);
            }
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS,all);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }
}
